package com.boot.param;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.constraints.*;

import com.boot.validate.custom.NameJsr;
import com.boot.validate.group.ValidateGroupNameOne;
import org.hibernate.validator.constraints.Length;

import lombok.Data;

@Data
public class Student implements Serializable {

    @NotBlank(groups = ValidateGroupNameOne.class, message = "JsrController姓名不能为空")
    @NotBlank(message = "name不能为空")
    @Length(min = 2, max = 10, message = "name的长度必须在{min}-{max}之间")
    private String              name;

    @NotNull(message = "年龄不能为空")
    @Min(value = 1, message = "年龄不能低于{value}岁")
    private String              age;

    @Positive(message = "学号必须是正数")
    private Integer             id;

    // 自定义校验器
    @NameJsr
    private String              alias;

    private List<@Email String> emails;

    public static void main(String[] args) {
        Student student = new Student();
        student.setName("T");
        student.setAge("");
        student.setId(-1);
        student.setAlias("Tom哥");
        student.setEmails(Arrays.asList("aalansehaiyang@gmail.com", "112129912"));
        Set<ConstraintViolation<Student>> result = Validation.buildDefaultValidatorFactory().getValidator().validate(student);

        List<String> message = result.stream().map(v -> v.getPropertyPath() + " " + v.getMessage() + ": "
                                                        + v.getInvalidValue()).collect(Collectors.toList());

        message.forEach(System.out::println);
    }
}
